<?php
/**
 * XuYuan Project
 *
 * @package	XueYuan 
 * @author	Jiuchi Team
 * @since	Version 1.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * User model class
 * @author Wayne Yu
 */
class Circle_user_model extends Jiuchi_Model{
	/**
	 * Construct function
	 */
	public function __construct() {
		parent::__construct();
		
		$this->table = 'circle_user';
	}
	
	/**
	 * Get circle members
	 * @param int $cid:Circle Id
	 * @param int $pageSize:Page Size
	 * @param int $last Item
	 */
	public function getCircleMembers($circleId, $pageSize = 0, $lastItem = 0){
		$count = -1;
		return $this->retrieve($count, $pageSize, $lastItem, 'user_id', array('circle_id'=>$circleId), NULL, NULL, NULL, array());
	}
	
	/**
	 * Get user circles
	 * @param int $uid:User Id
	 * @param int $pageSize:Page Size
	 * @param int $last Item
	 */
	public function getUserConcernedCircles($userId, $pageSize = 0, $lastItem = 0){
		$count = -1;
		return $this->retrieve($count, $pageSize, $lastItem, 'circle_id,is_joined', array('user_id'=>$userId), NULL, NULL, NULL, array());
	}
	
	public function checkIsMember($uid, $cid){
		$count = 1;
		$res = $this->retrieve($count, NULL, NULL, NULL, array('user_id' => $uid, 'circle_id' => $cid, 'is_joined' => 1));
		if($res){
			return TRUE;
		} else{
			return FALSE;
		}
	}
	
	public function checkIsConcerned($uid, $cid){
		$count = 1;
		$res = $this->retrieve($count, NULL, NULL, NULL, array('user_id' => $uid, 'circle_id' => $cid));
		if($res){
			return TRUE;
		} else{
			return FALSE;
		}
	}
	
	public function unconcern($uid, $cid){
		$res = $this->db->delete($this->table, array('user_id' => $uid, 'circle_id' => $cid));
		return $res;
	
		$count = 1;
		$res = $this->retrieve($count, NULL, NULL, NULL, array('user_id' => $uid, 'circle_id' => $cid));
		if(count($res)){
			$res = $this->db->delete($this->table, array('id' => $res[0]['id']));
			return $res;
		} else{
			return TRUE;
		}
	}
	
	public function joincircle($uid, $cid){
		$count = -1;
		$res = $this->retrieve($count, NULL, NULL, NULL, array('user_id' => $uid, 'circle_id' => $cid));
		if(count($res)){
			$res = $this->updateInfo($res[0]['id'], array('is_joined' => 1));
			return $res;
		} else{
			return FALSE;
		}
	}
	
	public function quitcircle($uid, $cid){
		$count = -1;
		$res = $this->retrieve($count, NULL, NULL, NULL, array('user_id' => $uid, 'circle_id' => $cid));
		if(count($res)){
			$res = $this->updateInfo($res[0]['id'], array('is_joined' => 0));
			return $res;
		} else{
			return FALSE;
		}
	}
}